<template>
  <div class="contain">
    <el-row :gutter="20">
      <el-col :span="8">
        <el-card class="box-card" :body-style="{ padding: '0px' }">
          <div slot="header" style="height: 10px" class="heder">
            <span>个人信息</span>
          </div>
          <div class="images">
            <img src="../assets/avatar.jpg" style="width: 100px;height: 100px;border-radius: 50%;line-height: 150px">
          </div>
          <div class="information">
            <div class="infor">
              <p><i class="fa fa-user-o" style="font-size: 14px;margin-right: 3px"></i>用户姓名</p>
              <p>{{ form.nickName }}</p>
            </div>
            <div class="infor">
              <p><i class="fa fa-intersex" style="font-size: 15px;margin-right: 3px"></i>性别</p>
              <p>{{form.sex==1?'男':'女'}}</p>
            </div>
            <div class="infor">
              <p><i class="fa fa-address-card-o" style="font-size: 14px;margin-right: 3px"></i>居住地</p>
              <p>{{ form.city }}</p>
            </div>
            <div class="infor">
              <p><i class="fa fa-mobile-phone" style="font-size: 18px;margin-right: 3px"></i>电话号码</p>
              <p>{{ form.tel }}</p>
            </div>
            <div class="infor">
              <p><i class="fa fa-calendar" style="font-size: 14px;margin-right: 3px"></i>创建日期</p>
              <p>{{ form.createTime }}</p>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="16">
        <el-card class="box-card" :body-style="{ padding: '0px' }">
          <div slot="header" style="height: 10px" class="heder">
            <span>基本资料</span>
          </div>
          <el-tabs v-model="activeName" @tab-click="handleClick" style="padding-left: 14px">
            <el-tab-pane label="基本资料" name="first">
              <el-form ref="formed" :model="formed" :rules="rules" label-width="80px" style="margin-right: 84px">
                <el-form-item label="用户名称" prop="nickName">
                  <el-input v-model="formed.nickName"></el-input>
                </el-form-item>
                <el-form-item label="手机号码" prop="tel">
                  <el-input v-model="formed.tel"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                  <el-radio-group v-model="formed.sex">
                    <el-radio :label="1" >男</el-radio>
                    <el-radio :label="0" >女</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="居住地" prop="city">
                  <el-input v-model="formed.city"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" size="small" @click="submitForm('formed')">保存</el-button>
                  <el-button type="danger" size="small" @click="close">关闭</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
            <el-tab-pane label="修改密码" name="second">
              <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px"
                       style="margin-right: 84px">
                <el-form-item label="密码" prop="oldpass">
                  <el-input type="password" v-model="ruleForm.oldPassword" autocomplete="off"
                            placeholder="请输入旧密码"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="pass">
                  <el-input type="password" v-model="ruleForm.newPassword" autocomplete="off"
                            placeholder="请输入新密码"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                  <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"
                            placeholder="请再次输入密码"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" size="small" @click="passwordsubmit('ruleForm')">提交</el-button>
                  <el-button type="danger" size="small" @click="close">关闭</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {getUserInfor, updateUserInfor,updatePassword} from "@/request/api";
import {mapMutations} from "vuex";

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.newPassword) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      activeName: 'first',
      form: {},
      formed: {},
      ruleForm: {
        oldPassword: '',
        newPassword: '',
        checkPass: '',
      },
      rules: {
        nickName: [
          {required: true, message: '请输入用户昵称', trigger: 'blur'},
          {min: 1, max: 10, message: '长度在 1到 10 个字符', trigger: 'blur'}
        ],
        tel: [
          {required: true, message: '请输入手机号码', trigger: 'blur'},
          {min: 11, max: 11, message: '长度在 11个字符', trigger: 'blur'}
        ],
        city: [
          {required: true, message: '请输入居住地', trigger: 'blur'},
          {min: 1, max: 11, message: '长度在 11个字符', trigger: 'blur'}
        ],
        sex: [
          {required: true, message: '请选择性别', trigger: 'change'}
        ],
        oldPassword: [
          {required: true, message: '请输入旧密码', trigger: 'blur'}
        ],
        pass: [
          {validator: validatePass, trigger: 'blur'}
        ],
        checkPass: [
          {validator: validatePass2, trigger: 'blur'}
        ],

      }
    }
  },
  created() {
    this.getUserinfor()
  },
  methods: {
    ...mapMutations(['getusername']),
    //获取基本资料
    getUserinfor() {
      getUserInfor().then(res => {
        if (res.code == 200) {
          let dt = Object.assign({}, res.data)
          this.form = dt
          this.formed = res.data
          return
        }
        this.$message.error(res.msg)
      }).catch(erro => {
        this.$message.error("请求失败")
      })
    },
    //进行提交基本资料修改
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const {id, nickName, sex, city} = this.formed
          updateUserInfor({id, nickName, sex, city}).then(res => {
            if (res.code == 200) {
              this.$message.success(res.msg)
              window.sessionStorage.setItem("nickName",nickName)
              this.getusername()
              this.getUserinfor()
              return
            }
            this.$message.error(res.msg)
          }).catch(erro => {
            this.$message.error("请求失败")
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //密码修改
    passwordsubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updatePassword(this.ruleForm).then(res => {
            if (res.code == 200) {
              this.$message.success(res.msg)
              window.sessionStorage.clear()
              this.$router.push("/login")
              return
            }
            this.$message.error(res.msg)
          }).catch(erro => {
            this.$message.error("请求失败")
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    close() {
      this.$router.push("/")
    },
    handleClick(tab, event) {
      console.log(tab, event);
    }
  }

}
</script>

<style scoped lang="less">
.contain {
  margin-top: 15px;
  margin-left: 15px;

  .heder {
    padding-top: 5px !important;
    padding-left: 2px !important;
    padding-bottom: 10px !important;
    font-size: 16px;
    font-weight: 600;
  }

  .images {
    width: 100%;
    height: 150px;
    position: relative;

    img {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }

  .information {
    width: 100%;

    .infor {
      margin-right: 10px;
      margin-left: 10px;
      display: flex;
      justify-content: space-between;
      font-size: 15px;
      border-top: 1px solid rgb(231, 234, 236);
    }
  }

}


</style>